import Vue from 'vue'
import VueRouter from 'vue-router'
import VuePageStack from 'vue-page-stack'
import HomeView from '../views/HomeView.vue'
import AboutView from '../views/AboutView.vue'
import FullView from '../views/FullView.vue'
import EmptyRedirectView from '../views/empty/EmptyRedirectView.vue'

Vue.use(VueRouter)

const routes = [{
		path: '/',
		name: 'home',
		component: HomeView,
		meta: {
			scrollTop: 0
		}
	},
	{
		path: '/about',
		name: 'about',
		component: AboutView,
		meta: {
			scrollTop: 0
		}
	},
	{
		// 空白页面用来处理app的跳转请求
		path: '/empty/redirect',
		name: 'empty-redirect',
		component: EmptyRedirectView
	},
	{
		path: '/full',
		name: 'full',
		component: FullView
	}
]

const createRouter = () => new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes,
	scrollBehavior(to, from, savedPosition) {
		if (from.name !== 'empty-redirect' && savedPosition) {
			return savedPosition
		} else {
			return { x: 0, y: 0 }
		}
	}
})

const router = createRouter()

Vue.use(VuePageStack, {
	router
});

export function resetRouter() {
	const newRouter = createRouter()
	router.matcher = newRouter.matcher // the relevant part
}

// let backlen = window.history.length - 1;
// window.history.go(-backlen);
// this.$router.replace({ path: '/HomePage' });
export default router;
